package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.*;
import com.diankangwo.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户地址控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "用户地址")
public class AddressController {
    @Autowired
    private UserDAO userDao;
    @Autowired
    private AddressDAO addressDao;
    @Autowired
    private CityDAO cityDao;
    @Autowired
    private ProvinceDAO provinceDao;
    @Autowired
    private DistrictDAO districtDao;

    /**
     * 获取我的收货地址列表
     *
     * @param accessToken token
     * @return
     */
    @ApiOperation(value="获取我的收获地址列表", notes="")
    @ApiImplicitParam(name="accessToken", value="token", required=true, dataType="String")
    @ResponseBody
    @RequestMapping(value="/address/getInfo", method=RequestMethod.GET)
    public String getInfo(String accessToken) {
        UserInfo user=userDao.getUserByToken(accessToken);
        String code="2";
        String msg="处理异常！";
        List<Address> addressList=null;
        if (user != null) {
            addressList=addressDao.findByUid(user.getUid());
            if (addressList != null) {
                code="1";
                msg="查询成功！";
            } else {
                code="1";
                msg="未查询到用户收货地址信息！";
            }
        } else {
            msg="未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, addressList);
    }

    /**
     * 新增我的收货地址信息
     *
     * @param accessToken token
     * @param name        名称
     * @param tel         电话
     * @param provinceID  省份ID
     * @param cityID      城市ID
     * @param countyID    区县ID
     * @param address     详细地址
     * @return
     */
    @ApiOperation(value="新增我的收货地址信息", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name="accessToken", value="token", required=true, dataType="String"),
            @ApiImplicitParam(name="name", value="名称", required=true, dataType="String"),
            @ApiImplicitParam(name="tel", value="电话", required=true, dataType="String"),
            @ApiImplicitParam(name="provinceID", value="省份ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="cityID", value="城市ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="countyID", value="区县ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="address", value="详细地址", required=true, dataType="String")
    })
    @ResponseBody
    @RequestMapping(value="/address/add", method=RequestMethod.POST)
    public String add(String accessToken, String name, String tel, Integer provinceID, Integer cityID, Integer countyID,
                      String address) {
        String code="2";
        String msg="处理异常！";
        if (accessToken == null || name == null || tel == null || provinceID == null || cityID == null
                || countyID == null || address == null) {
            msg="数据不完整！";
        } else {
            UserInfo user=userDao.getUserByToken(accessToken);
            if (user != null) {
                Address arss=new Address();
                arss.setPhone(tel);
                arss.setUid(user.getUid());
                arss.setDetailedAddress(address);
                arss.setCityId(cityDao.find(cityID));
                arss.setDistrictId(districtDao.find(countyID));
                arss.setProvinceId(provinceDao.find(provinceID));
                arss.setUsername(name);
                arss.setDefault(false);
                City city=cityDao.find(cityID);
                arss.setPostcode(city.getZipCode());
                if (addressDao.saveOrUpdate(arss) != null) {
                    code="1";
                    msg="增加成功！";
                } else {
                    msg="系统异常，增加失败！";
                }
            } else {
                msg="未查询到用户信息！";
            }
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 更新我的某条地址信息
     *
     * @param accessToken   token
     * @param id            id
     * @param name          姓名
     * @param tel           电话号码
     * @param provinceID    省ID
     * @param cityID        市ID
     * @param countyID      区域ID
     * @param address       详细地址
     * @param defaultAdress 是否默认地址
     * @return
     */
    @ApiOperation(value="更新我的某条地址信息", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name="accessToken", value="token", required=true, dataType="String"),
            @ApiImplicitParam(name="id", value="地址ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="provinceID", value="省份ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="cityID", value="城市ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="countyID", value="区县ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="address", value="详细地址", required=true, dataType="String"),
            @ApiImplicitParam(name="defaultAdress", value="是否默认地址", required=true, dataType="Boolean")
    })
    @ResponseBody
    @RequestMapping(value="/address/update", method=RequestMethod.POST)
    public String update(String accessToken, Integer id, String name, String tel, Integer provinceID, Integer cityID,
                         Integer countyID, String address, Boolean defaultAdress) {
        String code="2";
        String msg="处理异常！";
        if (accessToken == null || id == null || name == null || tel == null || provinceID == null || cityID == null
                || countyID == null || address == null || defaultAdress == null) {
            msg="数据不完整！";
        } else {
            UserInfo user=userDao.getUserByToken(accessToken);
            if (user != null) {
                Address add=new Address();
                District dis=districtDao.find(countyID);
                Province pro=provinceDao.find(provinceID);
                City city=cityDao.find(cityID);
                if (dis != null && pro != null && city != null) {
                    add.setCityId(city);
                    add.setDistrictId(dis);
                    add.setProvinceId(pro);
                    add.setDetailedAddress(address);
                    add.setPhone(tel);
                    add.setPostcode(city.getZipCode());
                    add.setUid(user.getUid());
                    add.setUsername(user.getUsername());
                    add.setDefault(defaultAdress);
                    if (addressDao.saveOrUpdate(add) != null) {
                        code="1";
                        msg="更新成功！";
                    } else {
                        msg="系统异常，更新失败!";
                    }
                } else {
                    msg="查询地址信息失败！";
                }
            } else {
                msg="未查询到用户信息！";
            }
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 删除我的指定地址信息
     *
     * @param accessToken token
     * @return
     */
    @ApiOperation(value="删除我的指定地址信息", notes="")
    @ApiImplicitParam(name="accessToken", value="token", required=true, dataType="String")
    @ResponseBody
    @RequestMapping(value="/address/del", method=RequestMethod.POST)
    public String del(String accessToken, String id) {
        UserInfo user=userDao.getUserByToken(accessToken);
        String code="2";
        String msg="处理异常！";
        if (user != null) {
            try {
                JSONArray json=JSONArray.fromObject(id); // 首先把字符串转成 JSONArray
                // 对象
                List l=JSONArray.toList(json);
                for (Object object : l) {
                    addressDao.deleteById((int)object);
                }
                code="1";
                msg="删除成功！";
            } catch (Exception e) {
                msg="删除异常!";
            }
        } else {
            msg="未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 获取地址列表接口
     *
     * @param provinceID 省份ID
     * @param cityID     城市ID
     * @return
     */
    @ApiOperation(value="获取地址列表接口", notes="")
    @ApiImplicitParams({
            @ApiImplicitParam(name="provinceID", value="省份ID", required=true, dataType="Integer"),
            @ApiImplicitParam(name="cityID", value="城市ID", required=true, dataType="Integer")
    })
    @ResponseBody
    @RequestMapping(value="/address/get_info_list", method=RequestMethod.GET)
    public String getInfoList(Integer provinceID, Integer cityID) {
        String code="2";
        String msg="处理异常！";
        List<Province> proList=null;
        List<City> cityList=null;
        List<District> districtList=null;
        if (provinceID != null) {
            cityList=cityDao.findByProId(provinceID);
            code="1";
            msg="查询城市数据成功！";
            return resultMsg.mapToJson(code, msg, cityList);
        } else if (cityID != null) {
            districtList=districtDao.findByCityId(cityID);
            code="1";
            msg="查询区域数据成功！";
            return resultMsg.mapToJson(code, msg, districtList);
        } else {
            code="1";
            proList=provinceDao.findAll();
            msg="查询省份数据成功！";
            return resultMsg.mapToJson(code, msg, proList);
        }
    }
}
